<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    *{
      margin: 0;
      padding: 0;
      list-style: none;
      text-decoration: none;
    }
    .nav{
      width: 100%;
      height: 60px;
      background-color: #ccc;
      line-height: 60px;
    }
    .w{
      width: 980px;
      height: 100%;
      background-color:salmon;
      margin: 0 auto;
    }
    li{
      float:left
    }
    a{
      display: block;
      padding: 0 20px;
    }
    .active{
      background-color: orange;
      color: black
    }
  </style>

</head>
<body>
  <div class="nav">
    <div class="w">
      <ul>
        <li><a href="javastript:;" class="active">首页</a></li>
        <li><a href="javastript:;">联系我们</a></li>
        <li><a href="javastript:;">关于我们</a></li>
        <li><a href="javastript:;">课程介绍</a></li>
      </ul>
    </div>
  </div>
  <script>
    var a_btns = document.querySelectorAll('a');
    for(var i = 0; i < a_btns.length; i++){
      a_btns[i].onclick = function(){
        //排他法
        // for(var j = 0; j < a_btns.length; j++){
        //   if(a_btns[j].classList.contains('active')==true){
        //     a_btns[j].classList.remove('active')
        //   }
        // }
        //获取具有active类名的标签 让后在移除这个类名
        document.querySelector('.active').classList.remove('active');
        this.classList.add('active');
      }
    }
  </script>
</body>
</html>